﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using SCResources.BusinissLayer;
using SCResources.General;
using Serie_Counter.Code;
using Serie_Counter.Forms;


namespace Serie_Counter.Presentatie
{
    public partial class TrayForm : Form
    {
        //Program vars
        private ScResources manager;
        

        //Forms
        private MainForm mainForm;
        private ArchiveForm archiveForm;
        private SettingsForm settingsForm;
        
        /// <summary>
        /// Default Constructor, ini the program
        /// </summary>
        /// 
        public TrayForm()
        {
            InitializeComponent();
            iniSerieCounter();
        }

        /// <summary>
        /// Destructor, save all info before closing
        /// </summary>
        ~TrayForm()
        {
            manager.saveCurrentContent();
        }

        private void iniSerieCounter()
        {
            //ini Manager
            manager = new ScResources();
            
            //todo: temp
            showForms(CallForm.Main);
            ////show active window when not as autoStart);
            //if (manager.getAutoStart().Equals(false))
            //{
            //    showForms(CallForm.Main);
            //}

            //create rememberMassage
            //if (manager.getEnableReminder())
            //{
            //    //@Todo show massages independent of the startup or not ?
            //    string balloonText = manager.getRememberMessage();
            //    if (!String.IsNullOrEmpty(balloonText))
            //    {
            //        showRememberBalloon(balloonText);
            //    }

            //}

            //check settings automove
            //toolStripMenuItemCheckFiles.Enabled = ScResources.Settings.EnableAutoMove;
            //ScResources.Settings.enableAutoMoveChanged += enableAutoMoveChanged;
        }

        #region Event methodes

        protected override void OnLoad(EventArgs e)
        {
            Visible = false;
            ShowInTaskbar = false;

            base.OnLoad(e);
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            showForms(CallForm.Main);
        }

        private void enableAutoMoveChanged(bool enableAutoMove)
        {
            toolStripMenuItemCheckFiles.Enabled = enableAutoMove;
        }

        #endregion


        #region Tooltip actions

        private void toolStripMenuItemActive_Click(object sender, EventArgs e)
        {
            showForms(CallForm.Main);
        }

        private void archiveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showForms(CallForm.Archived);
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showForms(CallForm.Settings);
        }

        private void toolStripMenuItemCheckFiles_Click(object sender, EventArgs e)
        {
            //check files once
            if(ScResources.Settings.SelectedMoveOption.Equals(AutoMoveOption.Manual))
            {
                //BL_FileMoving.checkFilesStatic(manager);
            }
            else
            {
                manager.checkFiles();
            }
        }

        private void toolStripMenuItemExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }


        #endregion

        #region Remember function

        private void showRememberBalloon(string serieText)
        {
            if (!String.IsNullOrEmpty(serieText)) notifyIcon.ShowBalloonTip(1000, "New available:", serieText, ToolTipIcon.Info);
        }

        #endregion


        #region Forms

        /// <summary>
        /// Show form delegate methode, to make sure every for can only be opend once
        /// </summary>
        /// <param name="selectedForm"></param>
        private void showForms(CallForm selectedForm)
        {
            switch (selectedForm)
            {
                case CallForm.Main:
                    if (mainForm == null)
                    {
                        CreateMainForm();
                    }
                    else
                    {
                        if (!mainForm.Visible)
                        {
                            CreateMainForm();
                        }
                    }
                    break;
                case CallForm.Archived:
                    if (archiveForm == null)
                    {
                        CreateArchivedForm();
                    }
                    else
                    {
                        if (!archiveForm.Visible)
                        {
                            CreateArchivedForm();
                        }
                    }
                    break;
                case CallForm.Settings:
                    if (settingsForm == null)
                    {
                        CreateSettingForm();
                    }
                    else
                    {
                        if (!settingsForm.Visible)
                        {
                            CreateSettingForm();
                        }
                    }
                    break;
            }
        }


        #region Create forms

        
        private void CreateSettingForm()
        {
            settingsForm = new SettingsForm(ScResources.Settings);
            if (settingsForm.ShowDialog() == DialogResult.OK)
            {
                ScResources.Settings = settingsForm.Settings;

                //make sure the change is made without the form needing te reopen (main form)
                if(settingsForm.Settings.CloseWithMainForm && mainForm.Visible)
                {
                    mainForm.Disposed += new EventHandler(toolStripMenuItemExit_Click); 
                }
                if (!settingsForm.Settings.CloseWithMainForm && mainForm.Visible)
                {
                    mainForm.Disposed -= new EventHandler(toolStripMenuItemExit_Click);
                }
            }
        }

        
        private void CreateArchivedForm()
        {
            archiveForm = new ArchiveForm(manager, new updateForm(updateForm));
            archiveForm.Show();
        }

        
        private void CreateMainForm()
        {
            mainForm = new MainForm(manager); 
            if (ScResources.Settings.CloseWithMainForm)
            {
                mainForm.Disposed += new EventHandler(toolStripMenuItemExit_Click);
            }
            mainForm.Show();
        }

        #endregion

        #region Form managment

        private void updateForm(SelectedCollection selectedCollection)
        {
            switch (selectedCollection)
            {
                case SelectedCollection.Active:
                    if (mainForm.Visible && archiveForm.Visible)
                    {
                        archiveForm.UCHelper.placeUserControls();
                    }
                    break;
                case SelectedCollection.Archived:
                    if (mainForm.Visible && archiveForm.Visible)
                    {
                        mainForm.UCHelper.placeUserControls();
                    }
                    break;
            }
        }

        #endregion

        

        

        

        #endregion
    }
}
